package au.edu.unsw.editingtool.db;

import java.sql.SQLException;
import java.util.Date;
import java.util.Hashtable;

import au.edu.unsw.editingtool.global.GlobalConstant;

public class DBEditingpool extends DBManager {

	protected void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("Publication", "pid");		
		MappingTable.put("Username", "username");		
		MappingTable.put("Date", "firstaccessdate");
		MappingTable.put("SessionID", "session_id");
	}
	
	public DBEditingpool() {
		this.table = "editing_pool";
		this.primaryKeys = new String[]{"Publication"};
		this.setupDBMapping();
	}
	
	public void addRecord(String pid, String username, String sessionid) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		Hashtable<String,String> record = new Hashtable<String,String>();
		record.put("Publication",pid);
		record.put("Username",username);		
		Long currenttime = (new Date()).getTime();
		record.put("Date", currenttime.toString());
		record.put("SessionID",sessionid);
		this.setColumnValues(record);		
		this.initDB();
		this.insert(false, false);
		this.clean();
	}
		
	public boolean isExist(String pid, String sessionid) throws SQLException, ClassNotFoundException {
		boolean result = false;
		this.setTable(this.table, "");
		String fields[] = {"Username"};		
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.setFieldsCondition("SessionID", this.setEqual(sessionid));	
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			System.out.println("[DBEDITINGPOOL] isEditing Resultset = NULL");
			this.clean();
			return result;
		}
		int count =0;
		while(ResultSet.next())
		{
			count++;
		}
		if(count==1)
		{				
			result = true;
		}
		this.clean();
		return result;
	}
	
	public String getUsername(String pid, String sessionid) throws SQLException, ClassNotFoundException {
		String username = null;
		this.setTable(this.table, "");
		String fields[] = {"Username"};		
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.setFieldsCondition("SessionID", this.setEqual(sessionid));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			System.out.println("[DBEDITINGPOOL] isEditing Resultset = NULL");
			this.clean();
			return username;
		}
		int count =0;		
		while(ResultSet.next())
		{
			count++;
			username = (String) ResultSet.getObject(1);
		}
		if(count==1)
		{		
			this.clean();
			return username;
		}
		this.clean();
		return null;
	}
	
	public String getUsername(String pid) throws SQLException, ClassNotFoundException {
		String username = null;
		this.setTable(this.table, "");
		String fields[] = {"Username"};		
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			System.out.println("[DBEDITINGPOOL] isEditing Resultset = NULL");
			this.clean();
			return username;
		}
		int count =0;		
		while(ResultSet.next())
		{
			count++;
			username = (String) ResultSet.getObject(1);
		}
		if(count==1)
		{		
			this.clean();
			return username;
		}
		this.clean();
		return null;
	}
	
	public void deleteRecord(String pid, String username, String sessionid) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.setFieldsCondition("Username", this.setEqual(username));
		this.setFieldsCondition("SessionID", this.setEqual(sessionid));
		this.initDB();
		this.deleteRecord();
		this.clean();
	}
	
	public void deleteRecord(String username, String sessionid) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Username", this.setEqual(username));
		this.setFieldsCondition("SessionID", this.setEqual(sessionid));
		this.initDB();
		this.deleteRecord();
		this.clean();
	}
	
	public void deleteRecord(String pid) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.initDB();
		this.deleteRecord();
		this.clean();
	}
}
